package com.example.project.controller;

import com.example.project.dto.TemplateDetailsPageDTO;
import com.example.project.entity.TemplateDetails;
import com.example.project.service.TemplateDetailsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (TemplateDetails)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@CrossOrigin
@RestController
@RequestMapping("templateDetails")
public class TemplateDetailsController {
    /**
     * 服务对象
     */
    @Resource
    private TemplateDetailsService templateDetailsService;

    /**
     * 分页查询
     *
     * param templateDetails 筛选条件
     * param pageRequest      分页对象
     * return 查询结果
     */

    @PostMapping("findPage")
    public ResponseEntity<Page<TemplateDetails>> queryByPage(@RequestBody TemplateDetailsPageDTO data) throws ParseException {
        com.example.project.entity.Page page = data.getPage();
        Date end=null;
        Date star=null;
        if(data.getMyTime2().getEndTime()!=null&&data.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(data.getMyTime2().getEndTime()+":00");
        }
        if(data.getMyTime2().getStartTime()!=null&&data.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(data.getMyTime2().getStartTime()+":00");

        }
        return ResponseEntity.ok(this.templateDetailsService.queryByPage(data.getTemplateDetails(), PageRequest.of(page.getPage(),page.getSize()), star, end));
    }
    @GetMapping("findAll")
    public  ResponseEntity<List<TemplateDetails>>   findAll( ){
        return ResponseEntity.ok(this.templateDetailsService.queryAll());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @PostMapping("{id}")
    public ResponseEntity<TemplateDetails> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.templateDetailsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param templateDetails 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<TemplateDetails> add(@RequestBody TemplateDetails templateDetails) {
        return ResponseEntity.ok(this.templateDetailsService.insert(templateDetails));
    }

    /**
     * 编辑数据
     *
     * @param templateDetails 实体
     * @return 编辑结果
     */

    @PutMapping("add")
    public ResponseEntity<TemplateDetails> edit(@RequestBody TemplateDetails templateDetails) {
        return ResponseEntity.ok(this.templateDetailsService.update(templateDetails));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */

    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody TemplateDetails data) {
        return ResponseEntity.ok(this.templateDetailsService.deleteById(data.getId()));
    }

}

